namespace Guia.Elements
{
    using System;
    using System.Windows.Automation;
    using Guia.Elements.PatternServants;
    using Guia.Locator;

    /// <summary>
    /// Represents a CalendarDayButtun.
    /// </summary>
    public class CalendarDayButton : UiElement, ISelectionItemMulti
    {
        /// <summary>
        /// Represents a helper-servant for the SelectionItem-pattern.
        /// </summary>
        private readonly ISelectionItemMulti selectionItemServant;

        /// <summary>
        /// Initializes a new instance of the <see cref="CalendarDayButton"/> class.
        /// </summary>
        /// <param name="automationElement">
        /// The automation element.
        /// </param>
        public CalendarDayButton(AutomationElement automationElement) : base(automationElement)
        {
            this.selectionItemServant = new SelectionItemImpl(this);
        }

        /// <summary>
        /// Gets the default-condition to find a control by its type.
        /// </summary>
        public static SearchCondition ControlTypeCondition
        {
            get { return SearchCondition.ByControlType(ControlType.Button).And(SearchCondition.ByClassName("CalendarDayButton")); }
        }

        #region ISelectionItemMulti Members

        /// <summary>
        /// Adds this item to the selection of its parent.
        /// </summary>
        /// <exception cref="InvalidOperationException">An InvalidOperationException is thrown, when this addition to the selection is not supported (e.g. because multi-select is not supported).</exception>
        public void AddToSelection()
        {
            this.selectionItemServant.AddToSelection();
        }

        #endregion

        #region ISelectionItemSingle Members

        /// <summary>
        /// Gets or sets a value indicating whether this item is selected or not.
        /// If multiple selection is allowed, this selection causes that only this item may be selected. Thus, other selected items may be unselected. Expect this item doesnt change its value throught the assignment.
        /// </summary>
        /// <exception cref="InvalidOperationException">This exception is thrown when this item can't be selected (e.g. because it is not enabled).</exception>
        public bool IsSelected
        {
            get
            {
                return this.selectionItemServant.IsSelected;
            }

            set
            {
                this.selectionItemServant.IsSelected = value;
            }
        }

        /// <summary>
        /// Unselects this item.
        /// </summary>
        /// <exception cref="InvalidOperationException">This exception is thrown when this item can't be selected/unselected (e.g. because it is not enabled).</exception>
        public void Unselect()
        {
            this.selectionItemServant.Unselect();
        }

        #endregion

        #region ISelectionItemSimple Members

        /// <summary>
        /// Selects this item.
        /// If multiple selection is allowed, this selection causes that only this item is selected. Thus, other selected items may become unselected.
        /// </summary>
        /// <exception cref="InvalidOperationException">This exception is thrown when this item can't be selected (e.g. because it is not enabled).</exception>
        public void Select()
        {
            this.selectionItemServant.Select();
        }

        #endregion
    }
}